<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>The Cache Manager - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.cache.cache.manager.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.cache.cache.manager.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.cache.backends.html">Zend_Cache Backends</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.cache.html">Zend_Cache</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.captcha.html">Zend_Captcha</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.cache.cache.manager" class="section"><div class="info"><h1 class="title">The Cache Manager</h1></div>
    

    <p class="para">
        It&#039;s the nature of applications to require a multitude of caches of any
        type often dependent on the controller, library or domain model being
        accessed. To allow for a simple means of defining <span class="classname">Zend_Cache</span>
        options in advance (such as from a bootstrap) so that accessing a cache
        object requires minimum setup within the application source code, the
        <span class="classname">Zend_Cache_Manager</span> class was written. This class
        is accompanied by <span class="classname">Zend_Application_Resource_Cachemanager</span>
        ensuring bootstrap configuration is available and
        <span class="classname">Zend_Controller_Action_Helper_Cache</span> to allow simple
        cache access and instantiation from controllers and other helpers.
    </p>

    <p class="para">
        The basic operation of this component is as follows. The Cache Manager allows users to setup
        &quot;option templates&quot;, basically options for a set of named caches. These can be set using the
        method  <span class="methodname">Zend_Cache_Manager::setCacheTemplate()</span>.
        These templates do not give rise to a cache until the user attempts to retrieve
        a named cache (associated with an existing option template) using the method
         <span class="methodname">Zend_Cache_Manager::getCache()</span>.
    </p>

    <pre class="programlisting brush: php">
$manager = new Zend_Cache_Manager;

$dbCache = array(
    &#039;frontend&#039; =&gt; array(
        &#039;name&#039; =&gt; &#039;Core&#039;,
        &#039;options&#039; =&gt; array(
            &#039;lifetime&#039; =&gt; 7200,
            &#039;automatic_serialization&#039; =&gt; true
        )
    ),
    &#039;backend&#039; =&gt; array(
        &#039;name&#039; =&gt; &#039;Core&#039;,
        &#039;options&#039; =&gt; array(
            &#039;cache_dir&#039; =&gt; &#039;/path/to/cache&#039;
        )
    )
);

$manager-&gt;setCacheTemplate(&#039;database&#039;, $dbCache);

/**
 * Anywhere else where the Cache Manager is available...
 */
$databaseCache = $manager-&gt;getCache(&#039;database&#039;);
</pre>


    <p class="para">
        The Cache Manager also allows simple setting of pre-instantiated caches
        using the method  <span class="methodname">Zend_Cache_Manager::setCache()</span>.
    </p>

    <pre class="programlisting brush: php">
$frontendOptions = array(
   &#039;lifetime&#039; =&gt; 7200,
   &#039;automatic_serialization&#039; =&gt; true
);

$backendOptions = array(
    &#039;cache_dir&#039; =&gt; &#039;/path/to/cache&#039;
);

$dbCache = Zend_Cache::factory(&#039;Core&#039;,
                             &#039;File&#039;,
                             $frontendOptions,
                             $backendOptions);

$manager = new Zend_Cache_Manager;
$manager-&gt;setCache(&#039;database&#039;, $dbCache);

/**
 * Anywhere else where the Cache Manager is available...
 */
$databaseCache = $manager-&gt;getCache(&#039;database&#039;);
</pre>


    <p class="para">
        If for any reason, you are unsure where the Cache Manager contains a
        pre-instantiated cache or a relevant option cache template to create one
        on request, you can check for the existence of a name cache configuration
        or instance using the method  <span class="methodname">Zend_Cache_Manager::hasCache()</span>.
    </p>

    <pre class="programlisting brush: php">
$manager = new Zend_Cache_Manager;

$dbCache = array(
    &#039;frontend&#039; =&gt; array(
        &#039;name&#039; =&gt; &#039;Core&#039;,
        &#039;options&#039; =&gt; array(
            &#039;lifetime&#039; =&gt; 7200,
            &#039;automatic_serialization&#039; =&gt; true
        )
    ),
    &#039;backend&#039; =&gt; array(
        &#039;name&#039; =&gt; &#039;Core&#039;,
        &#039;options&#039; =&gt; array(
            &#039;cache_dir&#039; =&gt; &#039;/path/to/cache&#039;
        )
    )
);

$manager-&gt;setCacheTemplate(&#039;database&#039;, $dbCache);

/**
 * Anywhere else where the Cache Manager is available...
 */
if ($manager-&gt;hasCache(&#039;database&#039;)) {
    $databaseCache = $manager-&gt;getCache(&#039;database&#039;);
} else {
    // create a cache from scratch if none available from Manager
}
</pre>


    <p class="para">
        In some scenarios, you may have defined a number of general use caches
        using <span class="classname">Zend_Cache_Manager</span> but need to fine-tune
        their options before use depending on the circumstances. You can edit
        previously set cache templates on the fly before they are instantiated
        using the method  <span class="methodname">Zend_Cache_Manager::setTemplateOptions()</span>.
    </p>

    <pre class="programlisting brush: php">
$manager = new Zend_Cache_Manager;

$dbCache = array(
    &#039;frontend&#039; =&gt; array(
        &#039;name&#039; =&gt; &#039;Core&#039;,
        &#039;options&#039; =&gt; array(
            &#039;lifetime&#039; =&gt; 7200,
            &#039;automatic_serialization&#039; =&gt; true
        )
    ),
    &#039;backend&#039; =&gt; array(
        &#039;name&#039; =&gt; &#039;Core&#039;,
        &#039;options&#039; =&gt; array(
            &#039;cache_dir&#039; =&gt; &#039;/path/to/cache&#039;
        )
    )
);

$manager-&gt;setCacheTemplate(&#039;database&#039;, $dbCache);

/**
 * Anywhere else where the Cache Manager is available...
 * Here we decided to store some upcoming database queries to Memcached instead
 * of the preconfigured File backend.
 */
$fineTuning = array(
    &#039;backend&#039; =&gt; array(
        &#039;name&#039; =&gt; &#039;Memcached&#039;,
        &#039;options&#039; =&gt; array(
            &#039;servers&#039; =&gt; array(
                array(
                    &#039;host&#039; =&gt; &#039;localhost&#039;,
                    &#039;port&#039; =&gt; 11211,
                    &#039;persistent&#039; =&gt; true,
                    &#039;weight&#039; =&gt; 1,
                    &#039;timeout&#039; =&gt; 5,
                    &#039;retry_interval&#039; =&gt; 15,
                    &#039;status&#039; =&gt; true,
                    &#039;failure_callback&#039; =&gt; &#039;&#039;
                )
            )
        )
    )
);
$manager-&gt;setTemplateOptions(&#039;database&#039;, $fineTuning);
$databaseCache = $manager-&gt;getCache(&#039;database&#039;);
</pre>


    <p class="para">
        To assist in making the Cache Manager more useful, it is accompanied by
        <span class="classname">Zend_Application_Resource_Cachemanager</span> and also
        the <span class="classname">Zend_Controller_Action_Helper_Cache</span> Action
        Helper. Both of these are described in their relevant areas of the
        Reference Guide.
    </p>

    <p class="para">
        Out of the box, <span class="classname">Zend_Cache_Manager</span> already includes
        three pre-defined cache templates called &quot;default&quot;, &quot;page&quot; and &quot;pagetag&quot;.
        The default cache is a simple File based cache using the Core frontend.
        The remaining two caches are used to implement a default Static Page Cache
        where static <acronym class="acronym">HTML</acronym>, <acronym class="acronym">XML</acronym> or even <acronym class="acronym">JSON</acronym>
        may be written to static files in <var class="filename">/public</var>. Control over a Static Page
        Cache is offered via <span class="classname">Zend_Controller_Action_Helper_Cache</span>, though you
        may alter the settings of this &quot;page&quot; the &quot;pagetag&quot; it uses to track tags using
         <span class="methodname">Zend_Cache_Manager::setTemplateOptions()</span> or even
         <span class="methodname">Zend_Cache_Manager::setCacheTemplate()</span> if overloading
        all of their options.
    </p>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.cache.backends.html">Zend_Cache Backends</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.cache.html">Zend_Cache</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.captcha.html">Zend_Captcha</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.cache.html">Zend_Cache</a></li>
  <li><a href="zend.cache.introduction.html">Introduction</a></li>
  <li><a href="zend.cache.theory.html">The Theory of Caching</a></li>
  <li><a href="zend.cache.frontends.html">Zend_Cache Frontends</a></li>
  <li><a href="zend.cache.backends.html">Zend_Cache Backends</a></li>
  <li class="active"><a href="zend.cache.cache.manager.html">The Cache Manager</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>